<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use October\Rain\Support\Facades\Input;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Models\RgRuleModel;
use Summer\Hjd\Models\WalletModel;
use Summer\Hjd\Models\WdRuleModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Summer\Hjd\Models\Settings as HjdSettings;
use Config;

class WdRuleController extends Controller
{
    use ApiReturn;

    /**
     * 提现规则
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $query=WdRuleModel::select();
        $query->with(['currency'=>function($q){
            $q->select('slug','title');
        }]);
        $query->whereHas('currency',function ($q){
            $q->where('status',1);
        });
        //可传参数，如果传则records[0]是该币种的配置
        if(Input::has('currency_slug')){
            $currency_slug=Input::get('currency_slug');
            $query->where('currency_slug',$currency_slug);
        }
        $query->where('status',1);
        $records=$query->get();
        $records->each(function ($record)use($user){
            $record->balance=WalletModel::where('currency_slug',$record->currency_slug)->where('user_id',$user->id)->value('balance');
        });
        $return['records']=$records;
        //收款类型
        $pay_types=[];
        $pay_type_config=Config::get('summer.hjd::wd_pay_type');
        foreach ($pay_type_config as $ptk=>$ptv){
            $arr['slug']=$ptk;
            $arr['title']=$ptv;
            if($ptk==1){
                $arr['number']=$user->bank_number??'请完善收款信息';
            }
            if($ptk==2){
                $arr['number']=$user->alipay_number??'请完善收款信息';
            }
            if($ptk==3){
                $arr['number']=$user->wechat_number??'请完善收款信息';
            }
            $pay_types[]=$arr;
        }
        $return['pay_types']=$pay_types;
        return $this->apiSuccess('操作成功',$return);
    }
}
